<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator"  >
          <a-button type="primary"  icon="plus" @click="$refs.addForm.add()">新增房产</a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a  @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm  placement="topRight" title="确认删除？" @confirm="() => busRealEstateDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { busRealEstatePage, busRealEstateDelete } from '@/api/modular/main/busrealestate/busRealEstateManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '房产名',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '房产类别 （字典 1集中式 2分散式 ）',
            align: 'center',
            dataIndex: 'category'
          },
          {
            title: '房产地址',
            align: 'center',
            dataIndex: 'address'
          },
          {
            title: '房产类型 （字典 house_type）',
            align: 'center',
            dataIndex: 'houseType'
          },
          {
            title: '电梯还是楼梯 （字典 1电梯 2楼梯）',
            align: 'center',
            dataIndex: 'elevator'
          },
          {
            title: '负责人',
            align: 'center',
            dataIndex: 'principal'
          },
          {
            title: '联系电话',
            align: 'center',
            dataIndex: 'phone'
          },
          {
            title: '楼层数',
            align: 'center',
            dataIndex: 'floors'
          },
          {
            title: '楼层房间数',
            align: 'center',
            dataIndex: 'rooms'
          },
          {
            title: '房号前缀',
            align: 'center',
            dataIndex: 'roompre'
          },
          {
            title: '房号后缀',
            align: 'center',
            dataIndex: 'roomsuf'
          },
          {
            title: '操作',
            width: '150px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return busRealEstatePage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {
    },
    methods: {
      busRealEstateDelete (record) {
        busRealEstateDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
